package com.activit.test;

import com.wuhong.entity.Student;
import org.activiti.engine.*;
import org.activiti.engine.impl.persistence.entity.TaskEntityImpl;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AcivitiDemo2
 * @Description 进阶业务，将Activiti和项目业务相关联
 * @Author director吴
 * @Date 2022/4/26 15:34
 * @Version 1.0
 **/
public class AcivitiDemo2 {


    /*
        1.添加业务中id到Activiti的表中
     */
    @Test
    public void test1() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //参数1：流程实例key,    参数2：BusinessKey 代笔业务的id
        //假设这里 “UUID1001”代表是一个人员发起的某种审批申请
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("evection", "UUID1001");

    }


    /*
       批量流程挂起、流程激活
     */
    @Test
    public void test2() {
        /**
         * 1.查询流程信息--当需要操作多个运行中的流程实例时，从流程定义的id中获取信息，比一个一个的获取流程运行实例更高效
         * 2.获取当前的信息是被挂起、或是激活转状态
         * 3.根据业务需求对不同状态的流程做操作
         * 单词--Suspended:暂停、activate：激活
         */
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();

        //获取流程的查询对象
        ProcessDefinition evection = repositoryService.createProcessDefinitionQuery().processDefinitionKey("evection").singleResult();

        //是否是挂起状态
        boolean suspended = evection.isSuspended();

        //如果是挂起，就激活、否则就挂起
        if (suspended) {
            //激活操作
            //参数1:流程Id、参数2：是否激活、参数3：时间（可以传null）
            repositoryService.activateProcessDefinitionById(evection.getId(), true, null);
        } else {
            //挂起操作
            //参数1:流程Id、参数2：是否挂起、参数3：时间（可以传null）
            repositoryService.suspendProcessDefinitionById(evection.getId(), true, null);
        }
    }

    /*
       单个实例挂起、激活操作
     */
    @Test
    public void test3() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();


        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId("22501").singleResult();
        System.out.println("AAA=====================");
        System.out.println(processInstance.toString());

        if (processInstance.isSuspended()) {

            runtimeService.activateProcessInstanceById("22501");

        } else {

            runtimeService.suspendProcessInstanceById("22501");
        }

        System.out.println("BBB=====================");
        System.out.println(runtimeService.createProcessInstanceQuery().processInstanceId("22501").toString());
    }

    /*
       完成个人任务
     */
    @Test
    public void test4() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        Task task = taskService.createTaskQuery().processInstanceId("22501").taskAssignee("张三").singleResult();

        System.out.println(task.getProcessInstanceId());//流程实例id
        System.out.println(task.getId());//流程任务id
        System.out.println(task.getAssignee());//当前审核人
        System.out.println(task.getName());//流程名字
        taskService.complete(task.getId());
    }


    /**
     * 部署bpmn,写有流程表达式的测试
     */
    @Test
    public void test5() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment().name("学员请假流程4").
                addClasspathResource("bpmn/student.bpmn").deploy();

    }


    /**
     * 启动流程-带有流程变量的流程实例测试
     */
    @Test
    public void test6() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();


        Student student = new Student();
        student.setDays(5);
        student.setName("张三");

        Map<String, Object> students = new HashMap<>();
        students.put("student", student);
        students.put("accounts2", "王经理");
        students.put("accounts3", "杨总经理");
        students.put("accounts4", "财务");


        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("studentProcess4", students);


        System.out.println("---------------------");
        System.out.println(processInstance.getId());


    }

    /**
     * 启动流程-带有流程变量的流程实例测试
     */
    @Test
    public void test7() {

        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        String key = "studentProcess2";

        TaskService taskService = defaultProcessEngine.getTaskService();

        Task task = taskService.createTaskQuery().processDefinitionKey(key).taskAssignee("李四").singleResult();

        System.out.println("taskId:" + task.getId());
        System.out.println(task.getDelegationState());


        Map<String, Object> students = new HashMap<>();

        taskService.complete(task.getId());


    }


    @Test
    public void test8() {

        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();

        String key = "studentProcess";
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

        TaskService taskService = defaultProcessEngine.getTaskService();
        Task task = taskService.createTaskQuery().processDefinitionKey(key).singleResult();

        Map<String, Object> students = new HashMap<>();
        Student student = new Student();
        student.setDays(5);
        student.setName("张三");
        students.put("student", student);


        runtimeService.setVariable(task.getId(), "student", students);


    }


    //组任务--拾取任务
    @Test
    public void test9() {

        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        //taskCandidateUser() 候选人

        Task task = taskService.createTaskQuery().processDefinitionKey("studentProcess4").taskCandidateUser("王五").singleResult();

        //王五拾取任务
        taskService.claim(task.getId(),"王五");
    }



    //组任务--归还任务
    @Test
    public void test10() {

        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        //候选人拾取后已经变成负责人了，所以要用taskAssignee 去查

        Task task = taskService.createTaskQuery().processDefinitionKey("studentProcess4").taskAssignee("王五").singleResult();

        //归还任务
        taskService.unclaim(task.getId());
    }


    //组任务--重新指定任务
    @Test
    public void test11() {

        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();

        //候选人拾取后已经变成负责人了，所以要用taskAssignee 去查

        Task task = taskService.createTaskQuery().processDefinitionKey("studentProcess4").taskAssignee("王五").singleResult();

        //归还任务
        taskService.unclaim(task.getId());
    }

}
